﻿using System;
using System.Xml.Serialization;
using PSE.Deployment.SetupBase.Configuration.Enums;
using System.Collections.Generic;
using System.Linq;

namespace PSE.Deployment.SetupBase.Configuration
{
    [Serializable()]
    public class XmlTagItemSource
    {
        [XmlAttribute()]
        public string Name { get; set; }

        [XmlAttribute()]
        public string TagValue { get; set; }

        [XmlAttribute()]
        public string TagItemValue { get; set; }

        [XmlAttribute()]
        public bool Selected { get; set; }
    }

    [Serializable]
    public class XmlTag
    {
        private string _value;

        [XmlAttribute()]
        public string Name { get; set; }

        [XmlAttribute()]
        public string Description { get; set; }

        [XmlAttribute()]
        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (!(string.IsNullOrEmpty(value) && string.IsNullOrEmpty(_value)) && value != _value)
                    HasChanges = true;

                if (this.ControlItemSource.Count > 0)
                {
                    for (int i = 0; i <= (this.ControlItemSource.Count - 1); i++)
                        ControlItemSource[i].Selected = (ControlItemSource[i].TagValue == value);
                }

                _value = value;
            }
        }

        [XmlAttribute()]
        public bool HasChanges { get; set; }

        public List<XmlXPath> XPathList { get; set; }

        [XmlAttribute()]
        public XmlTagType Type { get; set; }

        [XmlAttribute()]
        public XmlTagOperationType OperationType { get; set; }

        [XmlAttribute()]
        public bool IsList { get; set; }

        [XmlAttribute()]
        public string SQLCommand { get; set; }

        public List<XmlTagItemSource> ControlItemSource { get; set; }

        public XmlTagItemSource ControlItemSelected
        {
            get
            {
                return ControlItemSource.FirstOrDefault(x => x.Selected);
            }
        }

        public List<XmlAttribute> Attributes { get; set; }

        [XmlAttribute()]
        public bool IsVisible { get; set; }

        [XmlAttribute()]
        public bool IsEncrypted { get; set; }

        [XmlAttribute()]
        public CryptographyAlgorithm EncryptionAlgorithm { get; set; }

        [XmlAttribute()]
        public string AlgorithmPublicKey { get; set; }

        [XmlAttribute()]
        public bool IsRequired { get; set; }

        public XmlTag()
        {
            Attributes = new List<XmlAttribute>();
            XPathList = new List<XmlXPath>();
            ControlItemSource = new List<XmlTagItemSource>();
            IsVisible = true;
            IsRequired = true;
        }

        public bool HasAnyPropertyChanged
        {
            get
            {
                bool hasAtLeastOnePropertyChanged = false;

                if (Attributes != null && Attributes.Count > 0)
                {
                    foreach (XmlAttribute attribute in Attributes)
                    {
                        if (attribute.HasChanges)
                        {
                            hasAtLeastOnePropertyChanged = true;
                            break;
                        }
                    }
                }

                return hasAtLeastOnePropertyChanged;
            }
            set
            {
                if (Attributes != null && Attributes.Count > 0)
                {
                    foreach (XmlAttribute attribute in Attributes)
                    {
                        attribute.HasChanges = value;
                    }
                }
            }
        }
    }
}
